package dao

import (
	"github.com/jinzhu/gorm"
	"webapp/model"
)

var UserDao = newUserDao()

func newUserDao() *userDao{
	return &userDao{}
}

type userDao struct {

}

func (this *userDao) Take(db *gorm.DB, where ...interface{}) *model.User {
	ret := &model.User{}
	if err := db.Take(ret, where...).Error; err != nil {
		return nil
	}
	return ret
}

func (this *userDao) Create(db *gorm.DB, u *model.User) (err error) {
	err = db.Create(u).Error
	return
}

func (this *userDao)GetByUserName(db *gorm.DB, username string)  *model.User {
	return this.Take(db, "username = ?", username)
}

func (this *userDao) Delete(db *gorm.DB, username string) {
	db.Delete(&model.User{}, "username=?", username)
}

func (this *userDao) Update(db *gorm.DB, user *model.User) (err error){
	err = db.Save(user).Error
	return err
}